package rs.mbs2.epics.server.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@SuppressWarnings("serial")
@Entity
@Table(name="photo")
@NamedQueries({				
	@NamedQuery(name= "findPhotosByAlbumId",
				query="SELECT p FROM Photo p LEFT JOIN p.albums a WHERE a.id LIKE :albumId")
})

public class Photo implements Serializable {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "photo_id", unique = true, nullable = false)
	private Integer id;
	
	@Column(name = "photo_location", unique = false, nullable = false)
	private String  location;
	
	@Column(name = "photo_width", unique = false, nullable = false)
	private Integer width;
	
	@Column(name = "photo_height", unique = false, nullable = false)
	private Integer height;
	
	@Column(name = "photo_size", unique = false, nullable = false)
	private Long    size;
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "photo_upload_date", unique = false, nullable = false)
	private Date    uploadDate;
	
	@Column(name = "photo_title", unique = false, nullable = false)
	private String  title;
	
	@Column(name = "photo_description", unique = false, nullable = false)
	private String  description;
	
	@Column(name = "photo_public_available", unique = false, nullable = false)
	private Boolean publiclyAvailable;
	
	@Column(name = "photo_tags_allowed", unique = false, nullable = false)
	private Boolean tagsAllowed;
	
	@Column(name = "photo_notes_allowed", unique = false, nullable = false)
	private Boolean notesAllowed;
	
	@Column(name = "photo_comments_allowed", unique = false, nullable = false)
	private Boolean commentsAllowed;
	
	@Column(name = "photo_average_rating", unique = false, nullable = false)
	private Double averageRating;
	
	@Column(name = "photo_thumbnail_location", unique = false, nullable = false)
	private String  thumbnailLocation;
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "photo")
	private Set<Note> notes;
	/*
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "photo")
	private Set<Comment> comments;
	*/
	@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "photo")
	private Set<Comment> comments = new HashSet<Comment>();
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "photo")
	private Set<Rating> ratings;
	
	@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.PERSIST }, mappedBy = "photos")
	private Set<Album> albums;

	@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, mappedBy = "taggedPhotos")
	private Set<Tag> tags;
	
	
	public Photo() {
		
	}
	
	public Photo(String location, Integer width, Integer height, Long size,
			Date uploadDate, String title, String description,
			Boolean publiclyAvailable, Boolean tagsAllowed, Boolean notesAllowed,
			Boolean commentsAllowed, Double averageRating, String thumbnailLocation) {
		
		super();
		this.location = location;
		this.width = width;
		this.height = height;
		this.size = size;
		this.uploadDate = uploadDate;
		this.title = title;
		this.description = description;
		this.publiclyAvailable = publiclyAvailable;
		this.tagsAllowed = tagsAllowed;
		this.notesAllowed = notesAllowed;
		this.commentsAllowed = commentsAllowed;
		this.averageRating = averageRating;
		this.thumbnailLocation = thumbnailLocation;
		this.notes = new HashSet<>();
		this.comments = new HashSet<Comment>();
		this.ratings = new HashSet<>();
		this.albums = new HashSet<Album>();
		this.tags = new HashSet<>();
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result
				+ ((uploadDate == null) ? 0 : uploadDate.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Photo other = (Photo) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (uploadDate == null) {
			if (other.uploadDate != null)
				return false;
		} else if (!uploadDate.equals(other.uploadDate))
			return false;
		return true;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public Integer getWidth() {
		return width;
	}

	public void setWidth(Integer width) {
		this.width = width;
	}

	public Integer getHeight() {
		return height;
	}

	public void setHeight(Integer height) {
		this.height = height;
	}

	public Long getSize() {
		return size;
	}

	public void setSize(Long size) {
		this.size = size;
	}

	public Date getUploadDate() {
		return uploadDate;
	}

	public void setUploadDate(Date uploadDate) {
		this.uploadDate = uploadDate;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Boolean getPubliclyAvailable() {
		return publiclyAvailable;
	}

	public void setPubliclyAvailable(Boolean publiclyAvailable) {
		this.publiclyAvailable = publiclyAvailable;
	}

	public Boolean getTagsAllowed() {
		return tagsAllowed;
	}

	public void setTagsAllowed(Boolean tagsAllowed) {
		this.tagsAllowed = tagsAllowed;
	}

	public Boolean getNotesAllowed() {
		return notesAllowed;
	}

	public void setNotesAllowed(Boolean notesAllowed) {
		this.notesAllowed = notesAllowed;
	}

	public Boolean getCommentsAllowed() {
		return commentsAllowed;
	}

	public Double getAverageRating() {
		
		if(ratings != null && !ratings.isEmpty()) {
			Integer ratingSum = 0;
			
			for(Rating rating : ratings) {
				ratingSum += rating.getVote();
			}
			
			return (double) ratingSum / ratings.size();
		}
		
		return averageRating;
	}

	public void setAverageRating(Double averageRating) {
		this.averageRating = averageRating;
	}

	public void setCommentsAllowed(Boolean commentsAllowed) {
		this.commentsAllowed = commentsAllowed;
	}

	public String getThumbnailLocation() {
		return thumbnailLocation;
	}

	public void setThumbnailLocation(String thumbnailLocation) {
		this.thumbnailLocation = thumbnailLocation;
	}

	public Set<Note> getNotes() {
		return notes;
	}

	public void setNotes(Set<Note> notes) {
		this.notes = notes;
	}

	public Set<Comment> getComments() {
		return comments;
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}

	public Set<Rating> getRatings() {
		return ratings;
	}

	public void setRatings(Set<Rating> ratings) {
		this.ratings = ratings;
	}

	public Set<Album> getAlbums() {
		return albums;
	}

	public void setAlbums(Set<Album> albums) {
		this.albums = albums;
	}

	public Set<Tag> getTags() {
		return tags;
	}

	public void setTags(Set<Tag> tags) {
		this.tags = tags;
	}
	
}
